
// <copyright file="DbCommandExecuteException.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   This class represents an exception occurs when performing a Db operation.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Data
{
    using System;
    using System.Diagnostics;
    using System.Runtime.Serialization;
    using System.Security.Permissions;

    using SubhadraSolutions.Sharp.Utils.Validation;

    /// <summary>
    /// This class represents an exception occurs when performing a Db operation.
    /// </summary>
    [Serializable]
    public class DbCommandExecuteException : ApplicationException
    {
        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="DbCommandExecuteException"/> class.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <param name="innerException">
        /// The inner exception.
        /// </param>
        /// <param name="dataTransferObject">
        /// The dataTransferObject.
        /// </param>
        public DbCommandExecuteException(string message, Exception innerException, object dataTransferObject)
            : base(message, innerException)
        {
            this.DataTransferObject = dataTransferObject;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DbCommandExecuteException"/> class.
        /// </summary>
        /// <param name="info">
        /// The object that holds the serialized object data.
        /// </param>
        /// <param name="context">
        /// The contextual information about the source or destination.
        /// </param>
        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        protected DbCommandExecuteException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            string dtoTypeName = info.GetString("DtoTypeName");
            if (!string.IsNullOrEmpty(dtoTypeName))
            {
                try
                {
                    Type dtoType = Type.GetType(dtoTypeName);
                    if (dtoType != null)
                    {
                        this.DataTransferObject = info.GetValue("DataTransferObject", dtoType);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the dataTransferObject.
        /// </summary>
        /// <value>
        /// The dataTransferObject.
        /// </value>
        public object DataTransferObject { get; private set; }

        #endregion Public Properties

        #region Public Methods and Operators

        /// <summary>
        /// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with
        ///     information about the exception.
        /// </summary>
        /// <param name="info">
        /// The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object
        ///     data about the exception being thrown.
        /// </param>
        /// <param name="context">
        /// The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual
        ///     information about the source or destination.
        /// </param>
        /// <PermissionSet>
        ///     <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*"/>
        ///     <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="SerializationFormatter"/>
        /// </PermissionSet>
        /// <exception cref="System.ArgumentNullException">
        /// info
        /// </exception>
        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            Guard.ArgumentNotNull(info, "info");
            try
            {
                info.AddValue("DtoTypeName", this.DataTransferObject == null ? string.Empty : this.DataTransferObject.GetType().FullName);
                if (this.DataTransferObject != null)
                {
                    info.AddValue("DataTransferObject", this.DataTransferObject, this.DataTransferObject.GetType());
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            base.GetObjectData(info, context);
        }

        #endregion Public Methods and Operators
    }
}